* SPDX-License-Identifier: GPL-3.0-or-later
*/
+#define _GNU_SOURCE /* for strcasestr */
+#include <string.h>
+
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
}
static void
-prompt_metadata (char **uuid, char **name, char **description)
+prompt_metadata (char **uuid, char **name, char **description, char **template)
{
g_autoptr (GInputStream) stdin = NULL;
g_autoptr (GDataInputStream) istream = NULL;
if ((uuid == NULL || *uuid != NULL) &&
(name == NULL || *name != NULL) &&
- (description == NULL || *description != NULL))
+ (description == NULL || *description != NULL) &&
+ (template == NULL || *template != NULL))
return;
stdin = g_unix_input_stream_new (0, FALSE);
g_print ("\n");
}
+
+ if (template != NULL && *template == NULL)
+ {
+ g_autoptr (GPtrArray) templates = get_templates ();
+
+ if (templates->len == 1)
+ {
+ GDesktopAppInfo *info = g_ptr_array_index (templates, 0);
+ *template = g_desktop_app_info_get_string (info, TEMPLATE_KEY);
+ }
+ else
+ {
+ int i;
+
+ g_print (_("Choose one of the available templates:\n"));
+ for (i = 0; i < templates->len; i++)
+ {
+ GAppInfo *info = g_ptr_array_index (templates, i);
+ g_print ("%d) %-10s – %s\n",
+ i + 1,
+ g_app_info_get_name (info),
+ g_app_info_get_description (info));
+ }
+
+ while (*template == NULL)
+ {
+ g_autofree char *line = NULL;
+
+ g_print ("%s [1-%d]: ", _("Template"), templates->len);
+
+ line = g_data_input_stream_read_line_utf8 (istream, NULL, NULL, NULL);
+
+ if (line == NULL)
+ continue;
+
+ if (g_ascii_isdigit (*line))
+ {
+ long i = strtol (line, NULL, 10);
+
+ if (i > 0 && i <= templates->len)
+ {
+ GDesktopAppInfo *info;
+
+ info = g_ptr_array_index (templates, i - 1);
+ *template =
+ g_desktop_app_info_get_string (info, TEMPLATE_KEY);
+ }
+ }
+ else
+ {
+ for (i = 0; i < templates->len; i++)
+ {
+ GDesktopAppInfo *info = g_ptr_array_index (templates, i);
+ g_autofree char *cur_template = NULL;
+
+ cur_template =
+ g_desktop_app_info_get_string (info, TEMPLATE_KEY);
+
+ if (strcasestr (cur_template, line) != NULL)
+ *template = g_steal_pointer (&cur_template);
+ }
+ }
+ }
+ g_print ("\n");
+ }
+ }
}
int
}
if (interactive)
- prompt_metadata (&uuid, &name, &description);
+ prompt_metadata (&uuid, &name, &description, &template);
if (uuid == NULL || name == NULL || description == NULL)
{